<?php

namespace AuctionBack\Controller;

use AuctionCore\Common\AjaxPage;
use AuctionCore\Common\Util;
use AuctionCore\System\SystemLog;

class ScoreController extends BaseController
{

    private $questionScoreOrderLogic;

    private $clcwUserLogic;

    private $empId;

    public function __construct()
    {
        parent::__construct();

        $this->questionScoreOrderLogic = D('QuestionScoreOrder', 'Logic');

        $this->clcwUserLogic = D('ClcwUser', 'Logic');

        $this->empId = $this->user_info['emp_id'];
    }

    /**
     * 积分提问管理
     */
    public function questionScore()
    {
        $this->display();
    }

    /**
     * 获取提问积分订单
     */
    public function getOrderList()
    {
        $page = I('get.p');
        $size = 10;
        $params = [];
        $params['status'] = I('get.status', 1);
        $params['ord_num'] = I('get.ord_num');
        $params['question_id'] = I('get.question_id');
        $params['user_id'] = I('get.user_id');
        $params['nick_name'] = I('get.nick_name');
        $params['start_time'] = strtotime(I('get.start_time'));
        $params['end_time'] = strtotime(I('get.end_time', 'strtotime'));
        $result = $this->questionScoreOrderLogic->getQuestionScoreOrderList($page, $size, $params);
        $list = !empty($result['list']) ? $result['list'] : [];
        $Page = new AjaxPage($result['total'], $size, "get_page");
        $Page->setConfig('theme', " %totalRow% %header% %nowPage%/%totalPage% 页 %first% %upPage% %prePage% %linkPage% %nextPage% %downPage%  %end% %jumpform%");
        $show = $Page->show();// 分页显示输出
        $this->assign('page', $show);   // 赋值分页输出
        $this->assign('list', $list);
        $this->assign('p', $page);
        $this->assign('total', $result['total']);
        $this->assign('params', $params);
        $this->display();
    }


    /**
     * 积分订单详情
     */
    public function orderDetail()
    {

        $id = I('get.id');

        $order = $this->questionScoreOrderLogic->getQuestionSocreOrderInfo($id);

        $user = $this->clcwUserLogic->getUserInfoByUid($order['user_id']);

        $employee = M('employee')->where(['emp_id' => $order['emp_id']])->find();

        $this->assign('user', $user);
        $this->assign('employee', $employee);
        $this->assign('order', $order);
        $this->display();

    }

    /**
     * 提问积分退回
     */
    public function returnScore()
    {
        $id = I('get.id');

        $res = $this->questionScoreOrderLogic->returnQuestionScore($id);

        if ($res) {
            SystemLog::add(['emp_id' => $this->empId, 'action' => '积分提问退回积分']);

            echo Util::auto_jsonp_or_json($_REQUEST, ['code' => '200', 'msg' => '']);
        } else {
            echo Util::auto_jsonp_or_json($_REQUEST, ['code' => '5', 'msg' => '']);
        }
    }
}
